package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.order.model.response.InventoryInoutVO;
import com.wing.order.model.response.InventoryJobLabelVO;
import com.wing.order.model.response.OrderDeliverySignVO;
import com.wing.order.model.response.OrderInventorySignVO;
import com.wing.order.service.InventoryInoutService;
import com.wing.order.service.OrderDeliverySignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@Api(value = "Admin-入库签收管理", tags = "#/order/storageSign")
@RestController("storageSignController")
@RequestMapping("order/admin/storageSign")
@RequiredArgsConstructor
public class StorageSignController {
    private final InventoryInoutService inventoryInoutService;
    private final OrderDeliverySignService  orderDeliverySignService;

    @ApiOperation(value = "查询所有入库签收列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderDeliverySignVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , OrderDeliverySignVO orderDeliverySignVO
    ){
        //搜索
        IPage<OrderDeliverySignVO> list = orderDeliverySignService.getInventorySignVOPageList(page,limit,orderDeliverySignVO);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
    @ApiOperation(value = "查询所有入库计划", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "计划code", defaultValue = "", example = "", required = true,dataType = "String", paramType = "query")
    })
    @GetMapping("/getStoragePlanAll")
    public PageResult<InventoryInoutVO> getStoragePlanAll(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String code
    ){

        //搜索
        IPage<InventoryInoutVO> list = inventoryInoutService.getInventoryAllPageList(page,limit,code);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询入库计划签收的产品信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "入库计划id", required = true, dataType = "Long", paramType = "query")
    })
    @GetMapping("/getInventorySignProduct")
    public JsonResult<List<InventoryJobLabelVO>> getInventorySignProduct(Long planId) {
        List<InventoryJobLabelVO> list = inventoryInoutService.getInventorySignProduct(planId);
        return JsonResult.success(list);
    }
    @ApiOperation(value = "查询入库签收产品的包装条码", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobProductId", value = "入库签收产品id", required = true, dataType = "Long", paramType = "query")
    })
    @GetMapping("/getInventorySignPackLabel")
    public JsonResult<List<InventoryJobLabelVO>> getInventorySignPackLabel(Long jobProductId) {
        List<InventoryJobLabelVO> list = inventoryInoutService.getInventorySignPackLabel(jobProductId);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "签收包装条码", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signList", value="签收数据", dataType = "List",  required = true,paramType = "query")
    })
    @PostMapping("/packLabelSign")
    public JsonResult packLabelSign(@RequestBody List<ProductPackLabelVO> signList){
        try {
            inventoryInoutService.packLabelSign(signList);
            return JsonResult.success("签收成功");
        }catch (Exception e){
            log.error("packLabelSign fail",e);
            return JsonResult.fail("操作失败:" + e.getMessage());
        }
    }

}
